package com.coder.books.thinkinjava.concurrency;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

import static com.coder.util.Print.println;

/**
 *
 * @author yuhushuan
 * @date 2020/9/4 09:40
 */
class LiftOffRunner implements Runnable {
    private BlockingQueue<LiftOff> rockets;

    public LiftOffRunner(BlockingQueue<LiftOff> queue) {
        rockets = queue;
    }

    public void add(LiftOff lo) {
        try {
            rockets.put(lo);
        } catch (InterruptedException e) {
            println("Interrupted during put()");
        }
    }

    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {
                LiftOff rocket = rockets.take();
                rocket.run();
            }
        } catch (InterruptedException e) {
            println("waking from take()");
        }
        println("Exiting LiftOffRunner");
    }
}

class AddLiftOffRunnable implements Runnable {
    private BlockingQueue<LiftOff> rockets;
    private int number;

    public AddLiftOffRunnable(BlockingQueue<LiftOff> queue, int count) {
        rockets = queue;
        number = count;
    }

    @Override
    public void run() {
        for (int i = 0; i < number; i++){
            try {
                rockets.put(new LiftOff(5));
            } catch (InterruptedException e) {
                println("Interrupted during put()");
            }
        }
    }
}


public class TestBlockingQueues {

    static void test(String msg, BlockingQueue<LiftOff> queue) throws InterruptedException {
        println(msg);
        LiftOffRunner runner = new LiftOffRunner(queue);
        Thread t = new Thread(runner);
        t.start();

        for (int i = 0; i < 5; i++){
            runner.add(new LiftOff(5));
        }

        TimeUnit.SECONDS.sleep(2);
        t.interrupt();
        println("Finished " + msg + " test");
    }

    static void test1(String msg, BlockingQueue<LiftOff> queue) throws InterruptedException {
        println(msg);
        LiftOffRunner runner = new LiftOffRunner(queue);
        Thread t = new Thread(runner);
        t.start();

        AddLiftOffRunnable add = new AddLiftOffRunnable(queue, 5);
        Thread thread = new Thread(add);
        thread.start();

        TimeUnit.SECONDS.sleep(1);
        t.interrupt();
        thread.interrupt();
        println("Finished " + msg + " test");
    }

    public static void main(String[] args) throws InterruptedException {
//        test("LinkedBlockingDeque", new LinkedBlockingDeque<>());
//        test("ArrayBlockingQueue", new ArrayBlockingQueue<>(3));
//        test("SynchronousQueue", new SynchronousQueue<>());

        test1("LinkedBlockingDeque", new LinkedBlockingDeque<>());
        test1("ArrayBlockingQueue", new ArrayBlockingQueue<>(3));
        test1("SynchronousQueue", new SynchronousQueue<>());
    }

}
